package com.zretc.point.service;


import com.zretc.api.pojo.Point;
import com.zretc.api.pojo.User;
import com.zretc.common.constant.WebConstant;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import com.zretc.point.common.constant.PointConstant;
import com.zretc.point.dao.PointMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.sql.Timestamp;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Objects;


@Service
public class PointService {
    @Resource
    private PointMapper pointMapper;

    @Autowired
    private UserFeignClient userFeignClient;

    public ArrayList<Point> queryPointByUserId(User user){

        //System.out.println("service-------------"); // test
        //System.out.println(user.getUserId()); // test

        ArrayList<Point> points = pointMapper.getPointByUserId(user.getUserId());

        //System.out.println(points); // test

        return points;
    }

    public RespResult getPointByClockIn(Integer userId){

        System.out.println("PointService-----------------------------------------------------------------------"); // test

        // 获取本次签到时间
        Timestamp timeThisClockIn = new Timestamp(System.currentTimeMillis());

        if(Objects.isNull(userId))
        {
            return R.fail(WebConstant.NO_USER_ERROR_CODE, WebConstant.ERROR_301);
        }

        // 获得历史签到记录
        ArrayList<Point> historyClockIn = pointMapper.getPointClockIn(userId);

        System.out.println(historyClockIn); // test

        if(!Objects.isNull(historyClockIn))
        {

            // 获取最后一次签到的时间
            Point lastClockIn = historyClockIn.get(0);
            Timestamp timeLastClockIn = lastClockIn.getCreatedTime();

            //System.out.println(timeLastClockIn); // test

            // 将签到时间转换为日期
            Date dateLastClockIn = new Date(timeLastClockIn.getTime());
            Date dateThisClockIn = new Date(timeThisClockIn.getTime());

            System.out.println(dateLastClockIn.toString()); // test
            System.out.println(dateThisClockIn.toString()); // test

            // 若本次签到日期与最后一次签到日期相同，说明当天已签到
            if(dateLastClockIn.toString().equals(dateThisClockIn.toString()))
            {
                return R.fail(WebConstant.DUPLICATE_CLOCK_IN_ERROR_CODE,WebConstant.ERROR_310);
            }
        }

//        System.out.println(point); // test
        Point point = new Point();
        point.setUserId(userId);
        point.setUserAction(PointConstant.Action_ClockIn);
        point.setVariation(PointConstant.Variation_ClockIn);
        point.setCreatedTime(timeThisClockIn);

        User user = userFeignClient.getUserInfoById(userId);
//        System.out.println(user); // test
        user.setPoint(user.getPoint()+PointConstant.Variation_ClockIn);

//        System.out.println(point); // test
//        System.out.println(user); // test

        pointMapper.addPointAction(point);
        return userFeignClient.updatePointOrReputation(user);
    }

    public RespResult getPointByAddBlog(Integer userId){

        //System.out.println("PointService------------------------------------"); // test

        if(Objects.isNull(userId))
        {
            return R.fail(WebConstant.NO_USER_ERROR_CODE, WebConstant.ERROR_301);
        }

        // 从
        User user = userFeignClient.getUserInfoById(userId);

        Point thisPoint = new Point();
        thisPoint.setUserId(user.getUserId());
        thisPoint.setUserAction(PointConstant.Action_AddBlog);
        thisPoint.setVariation(PointConstant.Variation_AddBlog);
        thisPoint.setCreatedTime(new Timestamp(System.currentTimeMillis()));

        user.setPoint(user.getPoint()+PointConstant.Variation_AddBlog);

        System.out.println(thisPoint); // test
        System.out.println(user); //test

        pointMapper.addPointAction(thisPoint);
        return userFeignClient.updatePointOrReputation(user);

    }

    public RespResult getTotalPoint(Integer userId){
        if(Objects.isNull(userId))
        {
            return R.fail(WebConstant.NO_USER_ERROR_CODE, WebConstant.ERROR_301);
        }

        return R.success(userFeignClient.getPoint(userId));
    }

    public RespResult getReputation(Integer userId){
        if(Objects.isNull(userId))
        {
            return R.fail(WebConstant.NO_USER_ERROR_CODE, WebConstant.ERROR_301);
        }

        return R.success(userFeignClient.getReputation(userId));
    }
}
